home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-04-30 | 14.6 KB | 424 lines | [TEXT/MPCC] |
- //----------------------------------------------------------------------------------------
- // UI Events protocol
- //----------------------------------------------------------------------------------------
-
- #ifndef _CREDITNOWDEMO_
- #include "CPCreditNowDemo.h"
- #endif
- #include "Launchit.h"
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::InitPart(Environment* ev, ODStorageUnit* storageUnit, ODPart* self)
- {
- // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_InitPart
- EnteringMethod("\pCPCreditNowDemo::InitPart");
-
- this->PrivInitCreditNowDemo(ev, storageUnit, self);
-
- {
- CUsingLibraryResources fil;
- ::GetIndString(fTextData, kContentStringResID, kContentStringID);
- } // Use CUsingLibraryResources to use your part's resource fork.
- // Make it go out of scope when you are done.
-
- // Initialize your other fields here.
- fDirty = kODTrue;
- // Make sure our content is written out at least once.
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::InitPartFromStorage(Environment* ev, ODStorageUnit* storageUnit, ODPart* self)
- {
- // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_InitPartFromStorage
- EnteringMethod("\pCPCreditNowDemo::InitPartFromStorage");
-
- TRY
- // To allow editor swapping (translation) at runtime, OpenDoc requires
- // that we pass in a "reference" to ourselves when interacting with the
- // API (ie. WindowState::RegisterWindow(), Dispatcher::RegisterIdle, etc).
- // The "partWrapper" passed to us here and in InitPart is the
- // "reference" OpenDoc is asking us to use.
- fSelf = self;
-
- // Are we being opened from a read-only draft? If so, we cannot
- // write anything back out to our storage unit.
- fReadOnlyStorage = ( storageUnit->GetDraft(ev)->
- GetPermissions(ev) == kDPReadOnly );
-
- // Call the common initialization code to get set up.
- this->PrivInitCreditNowDemo(ev, storageUnit, self);
-
- // Read in the state the part was in when it was last Externalized.
- // This allows the part to present the same "environment" the user
- // had the part set up in the last time it was edited.
- this->PrivInternalizeStateInfo(ev, storageUnit);
-
- // Read in the contents for your part editor.
- this->PrivInternalizeContent(ev, storageUnit);
-
- SetTarget( 'CNOW', false );
-
- CATCH_ALL
- ODSetSOMException(ev, ErrorCode());
-
- // Clean up will occur in the destructor which will be called
- // shortly after we THROW the error.
- ENDTRY
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::PrivInitCreditNowDemo(Environment* ev, ODStorageUnit* storageUnit, ODPart* self)
- {
- EnteringMethod("\pCPCreditNowDemo::PrivInitCreditNowDemo");
-
- // Save off some references.
- fSelf = self;
- fStorageUnit = storageUnit;
-
- fSession = fStorageUnit->GetSession(ev);
- // As your part grows, add your initialization that is common
- // between InitPart and InitPartFromStorage here.
- // Note that GetStorageUnit can be used, since just prior to
- // calling this method, InitPart calledInitPersistentObject or
- // InitPartFromStorage called InitPersistentObjectFromStorage.
-
- fSelectionFocus = fSession->Tokenize(ev, kODSelectionFocus);
- fMenuFocus = fSession->Tokenize(ev, kODMenuFocus);
- fKeyFocus = fSession->Tokenize(ev, kODKeyFocus);
- fModalFocus = fSession->Tokenize(ev, kODModalFocus);
-
- fFocusSet = fSession->GetArbitrator(ev)->CreateFocusSet(ev);
- fFocusSet->Add(ev, fKeyFocus);
- fFocusSet->Add(ev, fMenuFocus);
- fFocusSet->Add(ev, fSelectionFocus);
-
- fMenuBar = fSession->GetWindowState(ev)->CopyBaseMenuBar(ev);
-
- #if 0
- rgac
- {
- CUsingLibraryResources fil;
- fMenu = ::GetMenu(kMenuID);
- if (fMenu)
- ::DetachResource((Handle)fMenu); // Must detach it!
- }
-
- if (!fMenu)
- DebugStr("\pCPCreditNowDemo::PrivInitCreditNowDemo -- couldn't create menu.");
- // We are using debug messages
- // for these types of errors. When SOM is available, this issue
- // will be addressed correctly.
-
- fMenuBar->AddMenuLast(ev, kMenuID, fMenu, fSelf);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::PrivInternalizeStateInfo(Environment* ev, ODStorageUnit* storageUnit)
- {
- // Description: This method could be used to read in settings
- // information for the part. This would be information
- // related to the workings of the part editor, not the
- // content.
-
- EnteringMethod("\pPrivInternalizeStateInfo");
-
- ODStorageUnitRef weakRef;
- ODULong size;
-
- // Internalize the part's display frame list.
-
- if ( storageUnit->Exists(ev, kODPropDisplayFrames, kODWeakStorageUnitRefs, 0) )
- {
- storageUnit->Focus(ev, kODPropDisplayFrames, kODPosUndefined,
- kODWeakStorageUnitRefs, 0, kODPosUndefined);
-
- size = storageUnit->GetSize(ev);
- storageUnit->SetOffset(ev, 0);
-
- for (ODULong offset = 0; offset < size; offset += 4)
- {
- TRY
- StorageUnitGetValue(storageUnit, ev, sizeof(ODStorageUnitRef), (ODPtr)&weakRef);
-
- if ( storageUnit->IsValidStorageUnitRef(ev, weakRef) )
- {
- ODFrame *frame = storageUnit->GetDraft(ev)->
- GetFrame(ev, storageUnit->GetIDFromStorageUnitRef(ev, weakRef));
- fDisplayFrames.Add(frame);
- }
- SOM_ENDTRY
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::PrivInternalizeContent(Environment* ev, ODStorageUnit* storageUnit)
- {
- // Description: This method is called during interalization of the
- // part. The content of the part should be read in.
- EnteringMethod("\pInternalizeContent");
-
- storageUnit->Focus(ev, kODPropContents, kODPosSame,
- kCreditNowDemoKind, 0, kODPosSame);
- fTextData[0] = (unsigned char)storageUnit->GetSize(ev);
- StorageUnitGetValue(storageUnit, ev, fTextData[0], &fTextData[1]);
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::Externalize(Environment* ev)
- {
- // CodeWarrior: Cmd-Click or Search:Find Definition • Explain_Externalize
- EnteringMethod("\pCPCreditNowDemo::Externalize");
-
- if (fDirty && !fReadOnlyStorage)
- {
- // Get our storage unit.
- ODStorageUnit* storageUnit = fSelf->GetStorageUnit(ev);
-
- // Verify that the storage unit has the appropriate properties
- // and values to allow us to run. If not, add them.
- this->PrivCheckAndAddProperties(ev, storageUnit);
-
- // Verify that there are no "bogus" values in the Content
- // property.
- this->PrivCleanContentProperty(ev, storageUnit);
-
- // Write out the part's state information.
- this->PrivExternalizeStateInfo(ev, storageUnit, 0, kODNULL);
-
- // Write out the part's content.
- this->PrivExternalizeContent(ev, storageUnit);
-
- // Update the "last modified" date and other part annotations.
- UpdateModificationInfo(ev, storageUnit);
-
- // Flag our part as no longer being dirty.
- fDirty = kODFalse;
- }
- }
-
- //----------------------------------------------------------------------------------------
-
- static ODBoolean PrivGetUserCatFromCat(ODNameSpaceManager* theNmSpcMgr,
- ODType category, ODIText** name)
- {
- ODBoolean result = kODFalse ;
-
- Environment* ev = somGetGlobalEnvironment();
-
- // look it up in the spaceName namespace
- ODObjectNameSpace* userStringNameSpace =
- (ODObjectNameSpace*)theNmSpcMgr->HasNameSpace( ev, kODCategoryUserString );
-
- if (userStringNameSpace)
- result = userStringNameSpace->GetEntry( ev, category, (ODObject**)name );
- return result ;
- }
-
- void CPCreditNowDemo::PrivCheckAndAddProperties(Environment* ev, ODStorageUnit* storageUnit)
- {
- // Description: This method is called during the creation of
- // stationery, to prepare a storage unit, and during
- // externalization, to verify that all the properties we
- // need are present.
- //
- // The part adds the default content property, a
- // preferred editor property (to aid in part binding),
- // and a default name for the part.
-
- EnteringMethod("\pPrivCheckAndAddProperties");
-
- // Create our content property and preferred content property kind.
-
- if ( !storageUnit->Exists(ev, kODPropContents, kODNULL, 0) )
- storageUnit->AddProperty(ev, kODPropContents);
- if ( !storageUnit->Exists(ev, kODPropContents, kCreditNowDemoKind, 0) )
- storageUnit->AddValue(ev, kCreditNowDemoKind);
-
- if ( !storageUnit->Exists(ev, kODPropPreferredKind, kODNULL, 0) )
- storageUnit->AddProperty(ev, kODPropPreferredKind);
- if ( !storageUnit->Exists(ev, kODPropPreferredKind, kODISOStr, 0) )
- {
- storageUnit->AddValue(ev, kODISOStr);
-
- // Since we are setting up the preferred kind property, we just write
- // out our default "kind" for the editor. We can write out the user
- // chosen kind in the PrivExternalizeStateInfo method.
-
- StorageUnitSetValue(storageUnit, ev, ODISOStrLength(kCreditNowDemoKind), kCreditNowDemoKind);
- }
-
- // Add our display frame list.
- if ( !storageUnit->Exists(ev, kODPropDisplayFrames, kODNULL, 0) )
- storageUnit->AddProperty(ev, kODPropDisplayFrames);
- if ( !storageUnit->Exists(ev, kODPropDisplayFrames, kODWeakStorageUnitRefs, 0) )
- storageUnit->AddValue(ev, kODWeakStorageUnitRefs);
-
- // Parts by default have no name, though one is visible from
- // the part info dialog and is required during Drag&Drop to the
- // Finder. We check for existence of the name property. If there
- // is none, we add it; if there is, we focus to it.
-
- if ( !storageUnit->Exists(ev, kODPropName, kODMacIText, 0) )
- {
- storageUnit->AddProperty(ev, kODPropName)->AddValue(ev, kODMacIText);
-
- // Generate a default name for the part using the category user string.
- ODNameSpaceManager* nsMgr = fSelf->GetStorageUnit(ev)->
- GetSession(ev)->GetNameSpaceManager(ev);
-
- // Get the category string from the category name space.
- ODIText* defaultName;
- if ( PrivGetUserCatFromCat(nsMgr, kODCategorySampledSound, &defaultName) )
- {
- if ( storageUnit->Exists(ev, kODPropName, kODMacIText, 0) )
- {
- ODSetITextProp(ev, storageUnit, kODPropName, kODMacIText, defaultName);
- }
- }
-
- // Do not dispose defaultName!!!
- }
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::PrivCleanContentProperty(Environment* ev, ODStorageUnit* storageUnit)
- {
- // Description: This method is called during exteralization of the
- // part. The part should remove any value in the content property
- // that it cannot "accurately" write to.
- EnteringMethod("\pPrivCleanContentProperty");
-
- storageUnit->Focus(ev, kODPropContents, kODPosUndefined, kODNULL, 0, kODPosAll);
-
- ODULong numValues = storageUnit->CountValues(ev);
-
- for (ODULong index = numValues; index > 1; index--)
- {
- // Index from 1 to n through the values.
- storageUnit->Focus(ev, kODPropContents, kODPosUndefined,
- kODNULL, index, kODPosUndefined);
- ODValueType value = storageUnit->GetType(ev);
-
- // If the value type is not one we support, remove it.
- if ( ODISOStrCompare(value, kCreditNowDemoKind) != 0 )
- storageUnit->Remove(ev);
- }
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::PrivExternalizeStateInfo(Environment* ev,
- ODStorageUnit* storageUnit,
- ODDraftKey key,
- ODFrame* scopeFrame)
- {
- // Description: This method is called during externalization of the
- // part. The current "state" of the editor should be
- // written out. This "state" information may be lost
- // during Data Interchange operations, so the part needs
- // to recover gracefully if information is missing or
- // incomplete.
-
- EnteringMethod("\pPrivExternalizeStateInfo");
-
- ODStorageUnitRef weakRef;
- ODFrame* frame;
- ODID frameID;
- ODID scopeFrameID = ( scopeFrame ? scopeFrame->GetID(ev) : 0 );
- ODDraft* fromDraft = fSelf->GetStorageUnit(ev)->GetDraft(ev);
-
- // Externalize the part's display frame list.
- storageUnit->Focus(ev, kODPropDisplayFrames, kODPosUndefined,
- kODWeakStorageUnitRefs, 0, kODPosUndefined);
-
- // Persistent object references are stored in a side table, rather than
- // in the property/value stream. Thus, deleting the contents of a value
- // will not "delete" the references previously written to that value. To
- // completely "delete" all references written to the value, we must
- // remove the value and add it back.
- storageUnit->Remove(ev);
- storageUnit->AddValue(ev, kODWeakStorageUnitRefs);
-
- for (FrameLink* link = fDisplayFrames.First(); link->Frame(); link = link->Next() )
- {
- frame = (ODFrame*) link->Frame();
- frameID = frame->GetID(ev);
-
- // If a draft key exists, then we are being cloned to another draft.
- // We must "weak" clone our display frame and reference the cloned
- // frame. The part re-uses the frameID variable so there aren't two
- // different GetWeakStorageUnitRef calls.
- if ( key )
- frameID = fromDraft->WeakClone(ev, key, frameID, 0, scopeFrameID);
-
- // Write out weak references to each of the part's display frames.
- storageUnit->GetWeakStorageUnitRef(ev, frameID, weakRef);
- TRY
- StorageUnitSetValue(storageUnit, ev, sizeof(ODStorageUnitRef), (ODPtr)&weakRef);
- SOM_ENDTRY
- }
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::PrivExternalizeContent(Environment* ev, ODStorageUnit* storageUnit)
- {
- // Description: This method is called during externalization of the
- // part. The content of the part should be written out.
- EnteringMethod("\pPrivExternalizeContent");
-
- storageUnit->Focus(ev, kODPropContents, kODPosSame,
- kCreditNowDemoKind, 0, kODPosSame);
- // Focus on content property.
-
- StorageUnitSetValue(fStorageUnit, ev, fTextData[0], &fTextData[1]);
- // Now we write out the property.
- }
-
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::PrivSetDirty(Environment* ev)
- {
- fDirty = kODTrue;
- fStorageUnit->GetDraft(ev)->SetChangedFromPrev(ev);
- }
-
-
-
- //AET<<
- //----------------------------------------------------------------------------------------
- // CPCreditNowDemo::ExternalizeSelection
- //----------------------------------------------------------------------------------------
-
- void CPCreditNowDemo::ExternalizeSelection(Environment* ev, ODStorageUnit* su, ODCloneKind cloneKind)
- {
- ODUnused(cloneKind);
- EnteringMethod("\pExternalizeSelection");
-
- Ptr data;
- long length;
- Handle text;
-
- if (fCurrentField->GetTextField()->HaveSelection())
- {
- su->Focus(ev, kODPropContents, kODPosSame, kCreditNowDemoKind, 0, kODPosSame);
- // First we focus on the property we want to write out.
-
- text = fCurrentField->GetTextField()->GetSelectedPtr(&data, &length);
- HLock(text);
- StorageUnitSetValue(su, ev, length, data);
- // Now we write out the property.
- HUnlock(text);
- }
- }
- //AET>>
-